package io.renren.modules.xm.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Maps;
import io.renren.common.utils.DateUtils;
import io.renren.modules.xm.entity.*;
import io.renren.modules.xm.service.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.xm.dao.CustomerDao;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {

    @Resource
    AccountService accountService;

    @Resource
    CustomerDao customerDao;

    @Resource
    AccPayDetailService accPayDetailService;

    @Resource
    GiftProjectService giftProjectService;

    @Resource
    OrderInfoService orderInfoService;

    @Resource
    ProdCategoryService prodCategoryService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        int current =  Integer.parseInt((String)params.get("page"));
        int limit =  Integer.parseInt((String)params.get("limit"));
        params.put("page",(current-1)*limit);
        params.put("limit",limit);

        int total = customerDao.count(params);
        Page<CustomerEntity> page = new Page<>(current,limit);
        List<CustomerEntity> list = customerDao.queryPage(params);
        page.setRecords(list);
        page.setTotal(total);

        return new PageUtils(page);
    }

    @Override
    public CustomerEntity selectById(Serializable id) {
        return customerDao.selectById(id);
    }

    @Override
    public Integer add(CustomerEntity customerEntity) {
        Long time = DateUtils.getCurrentTime();
        customerEntity.setCreateTi(time);
        customerEntity.setModifyTi(time);

        customerDao.add(customerEntity);
        if(customerEntity.getId() != null){
            AccountEntity accountEntity = new AccountEntity();
            accountEntity.setCustomerId(customerEntity.getId());
            accountEntity.setVipNo(customerEntity.getVipNo());
            accountEntity.setPayBalance(customerEntity.getPayBalance());
            accountEntity.setCreateTi(time);
            accountEntity.setModifyTi(time);
            accountEntity.setCreateUserId(customerEntity.getCreateUserId());
            accountEntity.setModifyUserId(customerEntity.getCreateUserId());
            accountService.insert(accountEntity);
            return 1;
        }

        return 0;
    }



    @Override
    public boolean updateAllColumnById(CustomerEntity customerEntity) {
//        Integer temp = customerDao.updatenById(customerEntity);
        Integer temp = customerDao.updateById(customerEntity);
        if(temp > 0){
            AccountEntity accountEntity = new AccountEntity();
            accountEntity.setCustomerId(customerEntity.getId());
            accountEntity.setVipNo(customerEntity.getVipNo());
            accountEntity.setPayBalance(customerEntity.getPayBalance());
            accountEntity.setModifyTi(customerEntity.getModifyTi());
            accountEntity.setModifyUserId(customerEntity.getCreateUserId());

            if(accountService.updateById(accountEntity)){
                return true;
            }
        }
        return false;
    }

    @Transactional
    @Override
    public Integer pay(CustomerEntity customer) {
        Long time = DateUtils.getCurrentTime();
        Integer userId = customer.getModifyUserId();
        OrderInfoEntity orderInfo = new OrderInfoEntity();

        ProdCategoryEntity prod = prodCategoryService.selectById(customer.getProductId());
        if(null != prod){
            orderInfo.setTotalDiscount(0.00);
            orderInfo.setTotalAmt(prod.getCategoryPrice());
            if(prod.getCategoryVipPrice() > 0.00){
                orderInfo.setTotalAmt(prod.getCategoryVipPrice());
                orderInfo.setTotalDiscount(prod.getCategoryPrice() - prod.getCategoryVipPrice());
            }
        }

        orderInfo.setCustomerId(customer.getId());
        orderInfo.setCategoryId(customer.getProductId());
        orderInfo.setPayStatusId(1);
        orderInfo.setWorkId(customer.getWorkId());
        orderInfo.setCreateTi(time);
        orderInfo.setModifyTi(time);
        orderInfo.setOrderTi(time);
        orderInfo.setCreateUserId(userId);
        orderInfo.setModifyUserId(userId);

        orderInfoService.insert(orderInfo);

        AccountEntity accountEntity = accountService.selectById(customer.getId());
        if(null != accountEntity && accountEntity.getPayBalance() > 0.00){
            accountEntity.setPayBalance(accountEntity.getPayBalance() - orderInfo.getTotalAmt());
            accountEntity.setModifyTi(time);
            accountEntity.setModifyUserId(userId);
            accountService.updateById(accountEntity);
            return 1;
        }
        return 0;
    }

    @Override
    public Integer recharge(CustomerEntity customerEntity) {
        Long time = DateUtils.getCurrentTime();
        Double balance = customerEntity.getBalance();
        Double preBalance    = customerEntity.getPayBalance();
        Double afterBalance = customerEntity.getBalance()+customerEntity.getPayBalance();
        Integer customerId = customerEntity.getId();
        Integer userId = customerEntity.getModifyUserId();

        AccPayDetailEntity t = new AccPayDetailEntity();
        t.setCustomerId(customerId);
        t.setPreBalance(preBalance);
        t.setAfterBalance(afterBalance);
        t.setPayBalance(balance);
        t.setCreateTi(time);
        t.setModifyTi(time);
        t.setCreateUserId(userId);
        t.setModifyUserId(userId);
        accPayDetailService.insert(t);

        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setCustomerId(customerId);
        accountEntity.setPayBalance(afterBalance);
        accountEntity.setModifyUserId(userId);
        accountEntity.setModifyTi(time);

        if(accountService.updateById(accountEntity)){
            return 1;
        }
        return 0;
    }

    @Override
    public Integer zengsong(CustomerEntity customer) {
        Long time = DateUtils.getCurrentTime();
        GiftProjectEntity giftProjectEntity = new GiftProjectEntity();
        giftProjectEntity.setCustomerId(customer.getId());
        giftProjectEntity.setCategoryId(customer.getProductId());
        giftProjectEntity.setVersion(customer.getZengsong());
        giftProjectEntity.setModifyTi(time);
        giftProjectEntity.setCreateTi(time);
        giftProjectEntity.setModifyUserId(customer.getModifyUserId());
        giftProjectEntity.setCreateUserId(customer.getModifyUserId());

        giftProjectService.insert(giftProjectEntity);
        return 0;
    }
}
